<?php
class Daftar extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');		
		$this->load->model('ipkd_model');		
	}
	
	function index()
	{
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		$colModel['detail'] = array('Detail',50,FALSE,'center',0);
		$colModel['TAHUN'] = array('Tahun',50,TRUE,'center',2);
		$colModel['NAMA_PEMOHON'] = array('Nama Pemohon',100,TRUE,'center',1);
		$colModel['ALAMAT_PEMOHON'] = array('Alamat',150,TRUE,'center',1);
		$colModel['TGL_PERMOHONAN'] = array('Tanggal Permohonan',100,TRUE,'center',1);
		$colModel['TGL_IJIN'] = array('Tanggal Perijinan',100,TRUE,'center',1);
		$colModel['NO_IJIN'] = array('No Ijin',100,TRUE,'center',1);
		$colModel['LOKASI'] = array('Lokasi',100,TRUE,'center',2);
		$colModel['KECAMATAN'] = array('Kecamatan',100,TRUE,'center',1);
		$colModel['KELURAHAN'] = array('Kelurahan / desa',100,TRUE,'center',1);
		$colModel['PEMANFAATAN'] = array('Pemanfaatan',150,TRUE,'center',1);
		$colModel['LUAS'] = array('LUAS (m2)',100,FALSE,'center',0);
		$colModel['MULAI_PERIJINAN'] = array('Mulai Perijinan',100,TRUE,'center',1);
		$colModel['SAMPAI_PERIJINAN'] = array('Sampai Perijinan',100,TRUE,'center',1);
		$colModel['RETRIBUSI'] = array('Retribusi',100,TRUE,'center',0);
		$colModel['TGL_PEMBAYARAN'] = array('Tanggal Pembayaran',100,TRUE,'center',1);
		
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '355',
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Menampilkan: {from} sampai {to} dari {total} data.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Keseluruhan Ijin Pemakaian Kekayaan Daerah'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		$buttons[] = array('Tambah','add','ipkd_js');
		$buttons[] = array('Hapus','delete','ipkd_js');
		$buttons[] = array('separator');
		$buttons[] = array('Pilih Semua','add','ipkd_js');
		$buttons[] = array('Deselect All','delete','ipkd_js');
		$buttons[] = array('separator');
        $buttons[] = array('Cetak Data','print','ipkd_js');
        $buttons[] = array('separator');
        $buttons[] = array('Lihat Jatuh Tempo','print','ipkd_js');
        $buttons[] = array('separator');
        $buttons[] = array('Habis Perijinan','print','ipkd_js');
        $buttons[] = array('separator');

		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("ipkd/daftar/daftar_grid"),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function ipkd_js(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Batal Memilih Semua')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}

				if (com=='Cetak Data')
                {
                    location.href='".base_url()."index.php/ipkd/daftar/cetak/'; 
                }

                if (com=='Lihat Jatuh Tempo')
                {
                    location.href='".base_url()."index.php/ipkd/daftar/tunggakan/'; 
                }

                if (com=='Habis Perijinan')
                {
                    location.href='".base_url()."index.php/ipkd/daftar/ijin/'; 
                }
				
				if (com=='Tambah')
				{
					location.href='".base_url()."index.php/ipkd/daftar/input_ipkd'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/ipkd/daftar/delete_ipkd')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
		
		/* $data['notification'] = "";
		if($this->session->userdata('notification')!=''){
			$data['notification'] = "
				<script>
					$(document).ready(function() {
						$.growlUI('Pesan :', '".$this->session->userdata('notification')."'); 
					});
				</script>
			";
		}//end if */	
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
		//$this->load->view('grid',$data);
		//$this->load->view('flexigrid',$data);
	}
	
	function daftar_grid()
	{
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('IDIPKD','TAHUN','NAMA_PEMOHON','ALAMAT','NO_IJIN','TGL_IJIN','PEMANFAATAN','TGL_PERMOHONAN','LOKASI','KECAMATAN','KELURAHAN','MULAI_PERIJINAN','SAMPAI_PERIJINAN','RETRIBUSI','TGL_PEMBAYARAN');
		
		$this->flexigrid->validate_post('IDIPKD','asc',$valid_fields);

		$records = $this->ipkd_model->get_ipkd_grid();
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 1;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->IDIPKD,
			$no,
			'<a href=\''.site_url().'/ipkd/daftar/edit_ipkd/'.$row->IDIPKD.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> ',
			'<a href=\''.site_url().'/ipkd/daftar/tampil/'.$row->IDIPKD.'/\' onclick="OpenPopup(this.href); return false"><img border=\'0\' src=\''.base_url().'images/main/77.png\'></a> ',			
			$row->TAHUN,
			$row->NAMA_PEMOHON,
			$row->ALAMAT_PEMOHON,
			date("d-m-Y",strtotime($row->TGL_PERMOHONAN)),
			date("d-m-Y",strtotime($row->TGL_IJIN)),
			$row->NO_IJIN,
			$row->LOKASI,
			$row->KECAMATAN,
			$row->KELURAHAN,
			$row->PEMANFAATAN,
			$row->LUAS,
			date("d-m-Y",strtotime($row->MULAI_PERIJINAN)),
			date("d-m-Y",strtotime($row->SAMPAI_PERIJINAN)),
			$row->RETRIBUSI,
			'<a href=\''.site_url().'/ipkd/daftar/pembayaran/'.$row->IDIPKD.'/\'>'.date("d-m-Y",strtotime($row->TGL_PEMBAYARAN)).'</a>'
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('tahun', 'Tahun', 'callback_cek_dropdown');
			$this->form_validation->set_rules('nama', 'NAMA PEMOHON', 'required');
			$this->form_validation->set_rules('alamat', 'ALAMAT PEMOHON', 'required');
			$this->form_validation->set_rules('tgl_perijinan', 'TANGGAL PERIJINAN', 'required');
			$this->form_validation->set_rules('tgl_permohonan', 'TANGGAL PERMOHONAN', 'required');
			$this->form_validation->set_rules('lokasi', 'LOKASI', 'required');
			$this->form_validation->set_rules('no_ijin', 'NO_IJIN', 'required');
			$this->form_validation->set_rules('kecamatan', 'KECAMATAN', 'callback_cek_dropdown_kcm');
			$this->form_validation->set_rules('kelurahan', 'KELURAHAN', 'callback_cek_dropdown_klr');
			$this->form_validation->set_rules('pemanfaatan', 'PEMANFAATAN', 'required');
			$this->form_validation->set_rules('luas', 'LOKASI', 'required|numeric');
			$this->form_validation->set_rules('mulai_perijinan', 'TANGGAL MULAI PERIJINAN', 'required');
			$this->form_validation->set_rules('sampai_perijinan', 'TANGGAL SAMPAI PERIJINAN', 'required');
			$this->form_validation->set_rules('retribusi', 'RETRIBUSI', 'required|numeric');
			$this->form_validation->set_rules('tgl_pembayaran', 'TANGGAL PEMBAYARAN', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!');
			$this->form_validation->set_message('numeric', 'Kolom %s hanya bisa diisi dengan menggunakan angka!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
			
	}
	
	function input_ipkd()
    {
		$kecamatan = $this->ipkd_model->get_all_kecamatan();
				
 		if ($this->cek_validasi() == FALSE){
				
				$kecamatan_options['0'] = '-- Pilih Kecamatan --';
					foreach($kecamatan->result() as $kcm){
					$kecamatan_options[$kcm->IDKECAMATAN] = $kcm->NAMA_KECAMATAN;
					}
				
				$kelurahan_options['0'] = '-- Pilih Kelurahan --';
				if(isset($_POST['kecamatan'])){
				$kelurahan = $this->ipkd_model->get_klr_by_kcm($this->input->post('kecamatan'));
					foreach($kelurahan->result() as $klr){
						$kelurahan_options[$klr->NAMA_KELURAHAN] = $klr->NAMA_KELURAHAN;
					}
				}
				
				$data['kecamatan_options'] = $kecamatan_options;
				$data['kelurahan_options'] = $kelurahan_options;
				$data['status'] = 'new';
				$data['content'] = $this->load->view('ipkd/form_ipkd',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$this->insert_ipkd();
			}            
    }
	
	function insert_ipkd(){
		
		$tgl1 = str_replace("/","-",$_REQUEST['tgl_perijinan']);
		$f_tgl_perijinan = date("Y-m-d", strtotime($tgl1));
		
		$tgl2 = str_replace("/","-",$_REQUEST['tgl_permohonan']);
		$f_tgl_permohonan = date("Y-m-d", strtotime($tgl2));
		
		$tgl3 = str_replace("/","-",$_REQUEST['mulai_perijinan']);
		$f_mulai_perijinan= date('Y-m-d', strtotime($tgl3));		
		
		$tgl4 = str_replace("/","-",$_REQUEST['sampai_perijinan']);
		$f_sampai_perijinan= date('Y-m-d', strtotime($tgl4));
		
		$tgl5 = str_replace("/","-",$_REQUEST['tgl_pembayaran']);
		$f_tgl_pembayaran= date('Y-m-d', strtotime($tgl5));
		
		$f_kcm = $this->ipkd_model->get_kcm_by_id($this->input->post('kecamatan'));
		
		foreach($f_kcm->result() as $kcm){
		$kcm_f = $kcm->NAMA_KECAMATAN;
		}
		
		$cek_foto_lokasi = $_FILES['foto_lokasi']['name'];
			if($cek_foto_lokasi != ""){
				
				if(!is_dir('./uploads/ipkd')){
					mkdir('./uploads/ipkd',0777);
				}
				
				// Upload Foto
				$config['upload_path'] = './uploads/ipkd/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
				$config['max_size']	= '2000';
				$config['encrypt_name']	= TRUE;
				
				$this->load->library('upload', $config);
				
				if(!$this->upload->do_upload('foto_lokasi')){
					$this->session->set_userdata('foto_lokasi', $this->upload->display_errors("<p>Error Foto : ", "</p>"));
					$data_file_lokasi = NULL;
					$valid_file = FALSE;
				}else{
					$data_file_lokasi = $this->upload->data();
					$valid_file = TRUE;
				}
				
				$file_foto_lokasi = $data_file_lokasi['file_name'];
			}else{
				$file_foto_lokasi = NULL;
				$valid_file = TRUE;
			}
			
		$cek_foto_sketsa = $_FILES['foto_sketsa']['name'];
			if($cek_foto_sketsa != ""){
				
				if(!is_dir('./uploads/ipkd')){
					mkdir('./uploads/ipkd',0777);
				}
				
				// Upload Foto
				$config['upload_path'] = './uploads/ipkd/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
				$config['max_size']	= '2000';
				$config['encrypt_name']	= TRUE;
				
				$this->load->library('upload', $config);
				
				if(!$this->upload->do_upload('foto_sketsa')){
					$this->session->set_userdata('foto_sketsa', $this->upload->display_errors("<p>Error Foto : ", "</p>"));
					$data_file_sketsa = NULL;
					$valid_file = FALSE;
				}else{
					$data_file_sketsa = $this->upload->data();
					$valid_file = TRUE;
				}
				
				$file_foto_sketsa = $data_file_sketsa['file_name'];
			}else{
				$file_foto_sketsa = NULL;
				$valid_file = TRUE;
			}
		
		$data = array
		(
			'TAHUN' 				=> $this->input->post('tahun'),
			'NAMA_PEMOHON' 			=> $this->input->post('nama'),
			'ALAMAT_PEMOHON' 		=> $this->input->post('alamat'),
			'TGL_IJIN' 				=> $f_tgl_perijinan,
			'TGL_PERMOHONAN' 		=> $f_tgl_permohonan,
			'LOKASI' 				=> $this->input->post('lokasi'),
			'NO_IJIN' 				=> $this->input->post('no_ijin'),
			'KECAMATAN' 			=> $kcm_f,
			'KELURAHAN' 			=> $this->input->post('kelurahan'),
			'PEMANFAATAN' 			=> $this->input->post('pemanfaatan'),
			'LUAS' 					=> $this->input->post('luas'),
			'MULAI_PERIJINAN'		=> $f_mulai_perijinan,
			'SAMPAI_PERIJINAN' 		=> $f_sampai_perijinan,
			'RETRIBUSI' 			=> $this->input->post('retribusi'),
			'TGL_PEMBAYARAN' 		=> $f_tgl_pembayaran,
			'UPLOAD_PATH_1' 		=> $file_foto_lokasi,
			'UPLOAD_PATH_2' 		=> $file_foto_sketsa,
			'STATUS' 				=> 1			
		);
		
		$this->ipkd_model->add_ipkd($data);
		$idtgl = $this->ipkd_model->cek_id_tgl();
		

		$data2 = array
		(
			'IDIPKD'			=> $idtgl,
			'TANGGAL_BAYAR' 	=> $f_tgl_pembayaran,
			'STATUS_BAYAR'		=> 0,
			'STATUS'			=> 1
		);

		$this->ipkd_model->add_tgl($data2);
		redirect('ipkd/daftar'); 
	}
	
	function edit_ipkd($id_ipkd)
    {
		$this->session->set_userdata('id_ipkd',$id_ipkd);
		$kecamatan = $this->ipkd_model->get_all_kecamatan();
		$all = $this->ipkd_model->get_id_ipkd($id_ipkd);
		
 		if ($this->cek_validasi() == FALSE){
				
				$kecamatan_options['0'] = '-- Pilih Kecamatan --';
					foreach($kecamatan->result() as $kcm){
					$kecamatan_options[$kcm->IDKECAMATAN] = $kcm->NAMA_KECAMATAN;
					}
				
				$kelurahan_options['0'] = '-- Pilih Kelurahan --';
				if(isset($_POST['kecamatan'])){
				$kelurahan = $this->ipkd_model->get_klr_by_kcm($this->input->post('kecamatan'));
					foreach($kelurahan->result() as $klr){
						$kelurahan_options[$klr->NAMA_KELURAHAN] = $klr->NAMA_KELURAHAN;
					}
				}
				foreach($all->result() as $row){
				$data['tahun'] = $row->TAHUN;
				$data['nama'] = $row->NAMA_PEMOHON;
				$data['alamat'] = $row->ALAMAT_PEMOHON;
				
				$tgl1 = $row->TGL_IJIN;
				$data['tgl_perijinan'] = date("d-m-Y", strtotime($tgl1));
				$tgl2 = $row->TGL_PERMOHONAN;
				$data['tgl_permohonan'] = date("d-m-Y", strtotime($tgl2));
				
				$data['lokasi'] = $row->LOKASI;
				$data['no_ijin'] = $row->NO_IJIN;
				$data['kecamatan'] = $row->KECAMATAN;
				$data['kelurahan'] = $row->KELURAHAN;
				$data['pemanfaatan'] = $row->PEMANFAATAN;
				$data['luas'] = $row->LUAS;
				
				$tgl3 = $row->MULAI_PERIJINAN;
				$data['mulai_perijinan'] = date("d-m-Y", strtotime($tgl3));
				$tgl4 = $row->SAMPAI_PERIJINAN;
				$data['sampai_perijinan'] = date("d-m-Y", strtotime($tgl4));
				$data['retribusi'] = $row->RETRIBUSI;
				$tgl5 = $row->TGL_PEMBAYARAN;
				$data['tgl_pembayaran'] = date("d-m-Y", strtotime($tgl5));
				}
				
				$data['foto_lokasi'] = $row->UPLOAD_PATH_1;
				$data['foto_sketsa'] = $row->UPLOAD_PATH_2;
				
				$data['kecamatan_options'] = $kecamatan_options;
				$data['kelurahan_options'] = $kelurahan_options;
				
				$data['id_ipkd']=$id_ipkd;
				$data['status'] = 'edit';
				$data['ipkd'] = $this->ipkd_model->get_id_ipkd($id_ipkd);
				$data['content'] = $this->load->view('ipkd/form_edit_ipkd',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$this->update_ipkd($id_ipkd);
			}            
    }
	
	function update_ipkd($id_ipkd){

		$tgl1 = str_replace("/","-",$_REQUEST['tgl_perijinan']);
		$f_tgl_perijinan = date("Y-m-d", strtotime($tgl1));
		
		$tgl2 = str_replace("/","-",$_REQUEST['tgl_permohonan']);
		$f_tgl_permohonan = date("Y-m-d", strtotime($tgl2));
		
		$tgl3 = str_replace("/","-",$_REQUEST['mulai_perijinan']);
		$f_mulai_perijinan= date('Y-m-d', strtotime($tgl3));		
		
		$tgl4 = str_replace("/","-",$_REQUEST['sampai_perijinan']);
		$f_sampai_perijinan= date('Y-m-d', strtotime($tgl4));
		
		$tgl5 = str_replace("/","-",$_REQUEST['tgl_pembayaran']);
		$f_tgl_pembayaran= date('Y-m-d', strtotime($tgl5));
		$f_tgl= date('Y', strtotime($tgl5));
		
		$f_kcm = $this->ipkd_model->get_kcm_by_id($this->input->post('kecamatan'));
		
		foreach($f_kcm->result() as $kcm){
		$kcm_f = $kcm->NAMA_KECAMATAN;
		}
		
		$cek_foto_lokasi = $_FILES['foto_lks']['name'];
			if($cek_foto_lokasi != ""){
				
				if(!is_dir('./uploads/ipkd')){
					mkdir('./uploads/ipkd',0777);
				}
				
				// Upload Foto
				$config['upload_path'] = './uploads/ipkd/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
				$config['max_size']	= '2000';
				$config['encrypt_name']	= TRUE;
				
				$this->load->library('upload', $config);
				
				if(!$this->upload->do_upload('foto_lks')){
					$data_file_lokasi = NULL;
					$valid_file = FALSE;
				}else{
					$data_file_lokasi = $this->upload->data();
					$valid_file = TRUE;
				}
				
				$file_foto_lokasi = $data_file_lokasi['file_name'];
			}else{
				$file_foto_lokasi = NULL;
				$valid_file = TRUE;
			}
			
		$cek_foto_sketsa = $_FILES['foto_sks']['name'];
			if($cek_foto_sketsa != ""){
				
				if(!is_dir('./uploads/ipkd')){
					mkdir('./uploads/ipkd',0777);
				}
				
				// Upload Foto
				$config['upload_path'] = './uploads/ipkd/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
				$config['max_size']	= '2000';
				$config['encrypt_name']	= TRUE;
				
				$this->load->library('upload', $config);
				
				if(!$this->upload->do_upload('foto_sks')){
					$data_file_sketsa = NULL;
					$valid_file = FALSE;
				}else{
					$data_file_sketsa = $this->upload->data();
					$valid_file = TRUE;
				}
				
				$file_foto_sketsa = $data_file_sketsa['file_name'];
			}else{
				$file_foto_sketsa = NULL;
				$valid_file = TRUE;
			}
		
		if($file_foto_lokasi && $file_foto_sketsa != NULL){
		$data = array(
			'TAHUN' 				=> $this->input->post('tahun'),
			'NAMA_PEMOHON' 			=> $this->input->post('nama'),
			'ALAMAT_PEMOHON' 		=> $this->input->post('alamat'),
			'TGL_IJIN' 				=> $f_tgl_perijinan,
			'TGL_PERMOHONAN' 		=> $f_tgl_permohonan,
			'LOKASI' 				=> $this->input->post('lokasi'),
			'NO_IJIN' 				=> $this->input->post('no_ijin'),
			'KECAMATAN' 			=> $kcm_f,
			'KELURAHAN' 			=> $this->input->post('kelurahan'),
			'PEMANFAATAN' 			=> $this->input->post('pemanfaatan'),
			'LUAS' 					=> $this->input->post('luas'),
			'MULAI_PERIJINAN'		=> $f_mulai_perijinan,
			'SAMPAI_PERIJINAN' 		=> $f_sampai_perijinan,
			'RETRIBUSI' 			=> $this->input->post('retribusi'),
			'TGL_PEMBAYARAN' 		=> $f_tgl_pembayaran,
			'UPLOAD_PATH_1' 		=> $file_foto_lokasi,
			'UPLOAD_PATH_2' 		=> $file_foto_sketsa	
		);}else{
			if($file_foto_lokasi==NULL){
			$data = array(
			'TAHUN' 				=> $this->input->post('tahun'),
			'NAMA_PEMOHON' 			=> $this->input->post('nama'),
			'ALAMAT_PEMOHON' 		=> $this->input->post('alamat'),
			'TGL_IJIN' 				=> $f_tgl_perijinan,
			'TGL_PERMOHONAN' 		=> $f_tgl_permohonan,
			'LOKASI' 				=> $this->input->post('lokasi'),
			'NO_IJIN' 				=> $this->input->post('no_ijin'),
			'KECAMATAN' 			=> $kcm_f,
			'KELURAHAN' 			=> $this->input->post('kelurahan'),
			'PEMANFAATAN' 			=> $this->input->post('pemanfaatan'),
			'LUAS' 					=> $this->input->post('luas'),
			'MULAI_PERIJINAN'		=> $f_mulai_perijinan,
			'SAMPAI_PERIJINAN' 		=> $f_sampai_perijinan,
			'RETRIBUSI' 			=> $this->input->post('retribusi'),
			'UPLOAD_PATH_2' 		=> $file_foto_sketsa,
			'TGL_PEMBAYARAN' 		=> $f_tgl_pembayaran);
			}elseif($file_foto_sketsa==NULL){
			$data = array(
			'TAHUN' 				=> $this->input->post('tahun'),
			'NAMA_PEMOHON' 			=> $this->input->post('nama'),
			'ALAMAT_PEMOHON' 		=> $this->input->post('alamat'),
			'TGL_IJIN' 				=> $f_tgl_perijinan,
			'TGL_PERMOHONAN' 		=> $f_tgl_permohonan,
			'LOKASI' 				=> $this->input->post('lokasi'),
			'NO_IJIN' 				=> $this->input->post('no_ijin'),
			'KECAMATAN' 			=> $kcm_f,
			'KELURAHAN' 			=> $this->input->post('kelurahan'),
			'PEMANFAATAN' 			=> $this->input->post('pemanfaatan'),
			'LUAS' 					=> $this->input->post('luas'),
			'MULAI_PERIJINAN'		=> $f_mulai_perijinan,
			'SAMPAI_PERIJINAN' 		=> $f_sampai_perijinan,
			'RETRIBUSI' 			=> $this->input->post('retribusi'),
			'UPLOAD_PATH_2' 		=> $file_foto_lokasi,
			'TGL_PEMBAYARAN' 		=> $f_tgl_pembayaran);

			}
		$data = array(
			'TAHUN' 				=> $this->input->post('tahun'),
			'NAMA_PEMOHON' 			=> $this->input->post('nama'),
			'ALAMAT_PEMOHON' 		=> $this->input->post('alamat'),
			'TGL_IJIN' 				=> $f_tgl_perijinan,
			'TGL_PERMOHONAN' 		=> $f_tgl_permohonan,
			'LOKASI' 				=> $this->input->post('lokasi'),
			'NO_IJIN' 				=> $this->input->post('no_ijin'),
			'KECAMATAN' 			=> $kcm_f,
			'KELURAHAN' 			=> $this->input->post('kelurahan'),
			'PEMANFAATAN' 			=> $this->input->post('pemanfaatan'),
			'LUAS' 					=> $this->input->post('luas'),
			'MULAI_PERIJINAN'		=> $f_mulai_perijinan,
			'SAMPAI_PERIJINAN' 		=> $f_sampai_perijinan,
			'RETRIBUSI' 			=> $this->input->post('retribusi'),
			'TGL_PEMBAYARAN' 		=> $f_tgl_pembayaran);
		}

		$all = $this->ipkd_model->get_id_ipkd($id_ipkd);
		foreach($all->result() as $row){

				$cek = $row->TGL_PEMBAYARAN;
				$cek_tgl = date("Y", strtotime($cek));
		}
		
		if($f_tgl != $cek_tgl){

			$data2 = array
			(
				'IDIPKD'			=> $id_ipkd,
				'TANGGAL_BAYAR' 	=> $f_tgl_pembayaran,
				'STATUS_BAYAR'		=> 0,
				'STATUS'			=> 1
			);

			$this->ipkd_model->add_tgl($data2);
		}
		else{

			$data2=array(
				'TANGGAL_BAYAR' => $f_tgl_pembayaran
				);

			$this->ipkd_model->update_tgl($id_ipkd,$data2);
		}

		$this->ipkd_model->edit_ipkd($id_ipkd,$data);
		redirect('ipkd/daftar');
	}
	
	//Delete
	function delete_ipkd()
	{
		$ipkd_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ipkd_ids_post_array as $index => $id_ipkd){
			if (isset($id_ipkd) && $id_ipkd != "") {
				$this->ipkd_model->delete_ipkd($id_ipkd);
						
			
			$msg .= "Ijin Pemakaian Kekayaan Daerah dengan id (".$id_ipkd.") telah berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
    
    //Print
    function cetak()
    {
        $data['ipkd'] = $this->ipkd_model->get_ipkd();
        $retribusi = $this->ipkd_model->get_sum_ipkd();
				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}

		$data['jumlah'] = $jumlah;
		$data['terbilang'] = ucfirst($this->_baca($jumlah));
        $header = '';
		header("Content-type: application/x-msdownload"); 
		header("Content-Disposition: attachment; filename=Rekap_ipkd.xls"); 
		header("Pragma: no-cache"); 
		header("Expires: 0");
		echo $header;
				
		$this->load->view('rekap/cetak_ipkd_all',$data);
    }

	function tampil($id_ipkd)
	{
		$record_ipkd = $this->ipkd_model->get_id_ipkd($id_ipkd);
			foreach ($record_ipkd->result() as $row){
                $data['tahun'] = $row->TAHUN;
				$data['nama'] = $row->NAMA_PEMOHON;
				$data['alamat'] = $row->ALAMAT_PEMOHON;
				
				$tgl1 = $row->TGL_IJIN;
				$data['tgl_perijinan'] = date("d-m-Y", strtotime($tgl1));
				$tgl2 = $row->TGL_PERMOHONAN;
				$data['tgl_permohonan'] = date("d-m-Y", strtotime($tgl2));
				
				$data['lokasi'] = $row->LOKASI;
				$data['no_ijin'] = $row->NO_IJIN;
				$data['kcm'] = $row->KECAMATAN;
				$data['klr'] = $row->KELURAHAN;
				$data['pemanfaatan'] = $row->PEMANFAATAN;
				$data['luas'] = $row->LUAS;

				$tgl3 = $row->MULAI_PERIJINAN;
				$data['mulai_perijinan'] = date("d-m-Y", strtotime($tgl3));
				$tgl4 = $row->SAMPAI_PERIJINAN;
				$data['sampai_perijinan'] = date("d-m-Y", strtotime($tgl4));
				$tgl5 = $row->TGL_PEMBAYARAN;
				$data['tgl_pembayaran'] = date("d-m-Y", strtotime($tgl5));
				

				$data['retribusi'] = $row->RETRIBUSI;
				$data['foto_1'] = $row->UPLOAD_PATH_1;
				$data['foto_2'] = $row->UPLOAD_PATH_2;
            }
		
		$this->load->view('ipkd/detail_ipkd',$data);
	}

	function _baca($n) {
		$str='';
		$this->dasar = array(1 => 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam','tujuh', 'delapan', 'sembilan');
		$this->angka = array(1000000000, 1000000, 1000, 100, 10, 1);
		$this->satuan = array('milyar', 'juta', 'ribu', 'ratus', 'puluh', '');

		$i = 0;
			if($n==0){
			   $str = "nol";
			}
			else{
			  while ($n != 0) {
				  $count = (int)($n/$this->angka[$i]);
			  if ($count >= 10) {
				  $str .= $this->_baca($count). " ".$this->satuan[$i]." ";
			  }else if($count > 0 && $count < 10){
				  $str .= $this->dasar[$count] . " ".$this->satuan[$i]." ";
			  }
			  $n -= $this->angka[$i] * $count;
			  $i++;
			   }
			   $str = preg_replace("/satu puluh (\w+)/i", "\\1 belas", $str);
			   $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i", "se\\1", $str);
			}
		return $str;
	}
	
	//cek_dropdown
	function cek_dropdown($value){
	$tahun= $this->input->post('tahun');
		if($tahun == 0){
			$this->form_validation->set_message('cek_dropdown', 'Kolom %s harus diisi!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function cek_dropdown_kcm($value){
	$kecamatan= $this->input->post('kecamatan');
		if($kecamatan == 0 || $kecamatan == '0' || $kecamatan ==''){
			$this->form_validation->set_message('cek_dropdown_kcm', 'Kolom %s harus diisi!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function cek_dropdown_klr($value){
	$kelurahan= $this->input->post('kelurahan');
	$cek_klr= $this->ipkd_model->get_klr_by_name($kelurahan);
	$c_klr='';
	foreach ($cek_klr->result() as $rows){
			$c_klr=$rows->NAMA_KELURAHAN;
		}
		if($c_klr != ''){
			return TRUE;
		}
		else{
			$this->form_validation->set_message('cek_dropdown_klr', 'Kolom %s harus diisi!!');
			return FALSE;
		}
	}
	
	function numeric_wcomma ($value){
		return preg_match('/^[0-9,]+$/', $str);
		}
 	
	function cek_data($value){
		$tahun= $this->input->post('tahun');

		$sql =  $this->db->query("SELECT COUNT(TAHUN) AS CEK FROM TAHUN WHERE TAHUN = '".$tahun."' AND STATUS = 1");
		
		$CEK='';
			
		foreach ($sql->result() as $rows){
			$CEK=$rows->CEK;
		}
		
		if($CEK==0){
				return TRUE;
		}else{
				$this->form_validation->set_message('cek_data', 'Data %s sudah diinisialisasikan!!!');
				return FALSE;
		}		
		
	}
  
	function cek_data_edit($value){
		$tahun= $this->input->post('tahun');
		
		$sql =  $this->db->query("SELECT COUNT(TAHUN) AS CEK FROM TAHUN WHERE TAHUN = '".$tahun."' AND STATUS = 1");
		$data = $this->tahun_model->get_tahunID($this->session->userdata('id_tahun'));
		
		
		$CEK='';
			
		foreach ($sql->result() as $rows){
			$CEK=$rows->CEK;
		}
		
		$tahun_lama='';
		foreach ($data->result() as $rows){
			$tahun_lama=$rows->TAHUN;
		}
		
		
		if($tahun == $tahun_lama)
		{
			return TRUE;
		}
		else{
			if($CEK==0){
					return TRUE;
			}else{
					$this->form_validation->set_message('cek_data_edit', 'Data %s sudah diinisialisasikan!!!');
					return FALSE;
			}		
		}
	}

  	function tunggakan()
	{
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		$colModel['detail'] = array('Detail',50,FALSE,'center',0);
		$colModel['TAHUN'] = array('Tahun',50,TRUE,'center',2);
		$colModel['NAMA_PEMOHON'] = array('Nama Pemohon',100,TRUE,'center',1);
		$colModel['ALAMAT_PEMOHON'] = array('Alamat',150,FALSE,'center',0);
		$colModel['TGL_PERMOHONAN'] = array('Tanggal Permohonan',100,TRUE,'center',1);
		$colModel['TGL_IJIN'] = array('Tanggal Perijinan',100,TRUE,'center',1);
		$colModel['NO_IJIN'] = array('No Ijin',100,FALSE,'center',1);
		$colModel['LOKASI'] = array('Lokasi',100,TRUE,'center',2);
		$colModel['KECAMATAN'] = array('Kecamatan',100,FALSE,'center',2);
		$colModel['KELURAHAN'] = array('Kelurahan/desa',100,FALSE,'center',2);
		$colModel['PEMANFAATAN'] = array('Pemanfaatan',150,FALSE,'center',0);
		$colModel['LUAS'] = array('LUAS (m2)',100,FALSE,'center',0);
		$colModel['MULAI_PERIJINAN'] = array('Mulai Perijinan',100,TRUE,'center',0);
		$colModel['SAMPAI_PERIJINAN'] = array('Sampai Perijinan',100,TRUE,'center',0);
		$colModel['RETRIBUSI'] = array('Retribusi',100,FALSE,'center',0);
		$colModel['TGL_PEMBAYARAN'] = array('Tanggal Pembayaran',100,TRUE,'center',0);
		
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '355',
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Menampilkan: {from} sampai {to} dari {total} data.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Jatuh Tempo Ijin Pemakaian Kekayaan Daerah'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		
        $buttons[] = array('separator');
        $buttons[] = array('Kembali Ke Daftar Ijin Pemakaian Kekayaan Daerah','','ipkd_js');
        $buttons[] = array('separator');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("ipkd/daftar/tunggakan_grid"),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function ipkd_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}

				if (com=='Cetak Data')
                {
                    location.href='".base_url()."index.php/ipkd/daftar/cetak/'; 
                }

                if (com=='Kembali Ke Daftar Ijin Pemakaian Kekayaan Daerah')
                {
                    location.href='".base_url()."index.php/ipkd/daftar'; 
                }
				
				if (com=='Tambah')
				{
					location.href='".base_url()."index.php/ipkd/daftar/input_ipkd'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/ipkd/daftar/delete_ipkd')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
		
		/* $data['notification'] = "";
		if($this->session->userdata('notification')!=''){
			$data['notification'] = "
				<script>
					$(document).ready(function() {
						$.growlUI('Pesan :', '".$this->session->userdata('notification')."'); 
					});
				</script>
			";
		}//end if */	
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
		//$this->load->view('grid',$data);
		//$this->load->view('flexigrid',$data);
	}
	
	function tunggakan_grid()
	{
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('IDIPKD','TAHUN','NAMA_PEMOHON','TGL_IJIN','TGL_PERMOHONAN','LOKASI','MULAI_PERIJINAN','SAMPAI_PERIJINAN','TGL_PEMBAYARAN');
		
		$this->flexigrid->validate_post('IDIPKD','asc',$valid_fields);

		$records = $this->ipkd_model->get_tunggakan_grid();
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 1;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->IDIPKD,
			$no,
			'<a href=\''.site_url().'/ipkd/daftar/edit_ipkd/'.$row->IDIPKD.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> ',
			'<a href=\''.site_url().'/ipkd/daftar/tampil/'.$row->IDIPKD.'/\' onclick="OpenPopup(this.href); return false"><img border=\'0\' src=\''.base_url().'images/main/77.png\'></a> ',			
			$row->TAHUN,
			$row->NAMA_PEMOHON,
			$row->ALAMAT_PEMOHON,
			date("d-m-Y",strtotime($row->TGL_PERMOHONAN)),
			date("d-m-Y",strtotime($row->TGL_IJIN)),
			$row->NO_IJIN,
			$row->LOKASI,
			$row->KECAMATAN,
			$row->KELURAHAN,
			$row->PEMANFAATAN,
			$row->LUAS,
			date("d-m-Y",strtotime($row->MULAI_PERIJINAN)),
			date("d-m-Y",strtotime($row->SAMPAI_PERIJINAN)),
			$row->RETRIBUSI,
			'<a href=\''.site_url().'/ipkd/daftar/pembayaran/'.$row->IDIPKD.'/\'>'.date("d-m-Y",strtotime($row->TGL_PEMBAYARAN)).'</a>'
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}

	function ijin()
	{
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		$colModel['detail'] = array('Detail',50,FALSE,'center',0);
		$colModel['TAHUN'] = array('Tahun',50,TRUE,'center',2);
		$colModel['NAMA_PEMOHON'] = array('Nama Pemohon',100,TRUE,'center',1);
		$colModel['ALAMAT_PEMOHON'] = array('Alamat',150,FALSE,'center',0);
		$colModel['TGL_PERMOHONAN'] = array('Tanggal Permohonan',100,TRUE,'center',1);
		$colModel['TGL_IJIN'] = array('Tanggal Perijinan',100,TRUE,'center',1);
		$colModel['NO_IJIN'] = array('No Ijin',100,FALSE,'center',1);
		$colModel['LOKASI'] = array('Lokasi',100,TRUE,'center',2);
		$colModel['KECAMATAN'] = array('Kecamatan',100,FALSE,'center',2);
		$colModel['KELURAHAN'] = array('Kelurahan/desa',100,FALSE,'center',2);
		$colModel['PEMANFAATAN'] = array('Pemanfaatan',150,FALSE,'center',0);
		$colModel['LUAS'] = array('LUAS (m2)',100,FALSE,'center',0);
		$colModel['MULAI_PERIJINAN'] = array('Mulai Perijinan',100,TRUE,'center',0);
		$colModel['SAMPAI_PERIJINAN'] = array('Sampai Perijinan',100,TRUE,'center',0);
		$colModel['RETRIBUSI'] = array('Retribusi',100,FALSE,'center',0);
		$colModel['TGL_PEMBAYARAN'] = array('Tanggal Pembayaran',100,TRUE,'center',0);
		
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '355',
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Menampilkan: {from} sampai {to} dari {total} data.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Habis Perijinan'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		
        $buttons[] = array('separator');
        $buttons[] = array('Kembali Ke Daftar','','ipkd_js');
        $buttons[] = array('separator');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("ipkd/daftar/ijin_grid"),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function ipkd_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}

				if (com=='Cetak Data')
                {
                    location.href='".base_url()."index.php/ipkd/daftar/cetak/'; 
                }

                if (com=='Kembali Ke Daftar')
                {
                    location.href='".base_url()."index.php/ipkd/daftar'; 
                }
				
				if (com=='Tambah')
				{
					location.href='".base_url()."index.php/ipkd/daftar/input_ipkd'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/ipkd/daftar/delete_ipkd')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
		
		/* $data['notification'] = "";
		if($this->session->userdata('notification')!=''){
			$data['notification'] = "
				<script>
					$(document).ready(function() {
						$.growlUI('Pesan :', '".$this->session->userdata('notification')."'); 
					});
				</script>
			";
		}//end if */	
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
		//$this->load->view('grid',$data);
		//$this->load->view('flexigrid',$data);
	}
	
	function ijin_grid()
	{
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('IDIPKD','TAHUN','NAMA_PEMOHON','TGL_IJIN','TGL_PERMOHONAN','LOKASI','MULAI_PERIJINAN','SAMPAI_PERIJINAN','TGL_PEMBAYARAN');
		
		$this->flexigrid->validate_post('IDIPKD','asc',$valid_fields);

		$records = $this->ipkd_model->get_ijin_grid();
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 1;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->IDIPKD,
			$no,
			'<a href=\''.site_url().'/ipkd/daftar/edit_ipkd/'.$row->IDIPKD.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> ',
			'<a href=\''.site_url().'/ipkd/daftar/tampil/'.$row->IDIPKD.'/\' onclick="OpenPopup(this.href); return false"><img border=\'0\' src=\''.base_url().'images/main/77.png\'></a> ',			
			$row->TAHUN,
			$row->NAMA_PEMOHON,
			$row->ALAMAT_PEMOHON,
			date("d-m-Y",strtotime($row->TGL_PERMOHONAN)),
			date("d-m-Y",strtotime($row->TGL_IJIN)),
			$row->NO_IJIN,
			$row->LOKASI,
			$row->KECAMATAN,
			$row->KELURAHAN,
			$row->PEMANFAATAN,
			$row->LUAS,
			date("d-m-Y",strtotime($row->MULAI_PERIJINAN)),
			date("d-m-Y",strtotime($row->SAMPAI_PERIJINAN)),
			$row->RETRIBUSI,
			'<a href=\''.site_url().'/ipkd/daftar/pembayaran/'.$row->IDIPKD.'/\'>'.date("d-m-Y",strtotime($row->TGL_PEMBAYARAN)).'</a>'
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}

	function pembayaran($id_ipkd)
	{
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['TANGGAL_BAYAR'] = array('Tanggal Pembayaran',150,TRUE,'center',2);
		$colModel['STATUS_BAYAR'] = array('Status Bayar',100,TRUE,'center',0);
		
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '355',
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Menampilkan: {from} sampai {to} dari {total} data.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Keseluruhan Tanggal Pembayaran'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		*

		$buttons[] = array('Tambah','add','ipkd_js');
		$buttons[] = array('Hapus','delete','ipkd_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','ipkd_js');
		$buttons[] = array('Deselect All','delete','ipkd_js');
		$buttons[] = array('separator');
        $buttons[] = array('Cetak Data','print','ipkd_js');
        $buttons[] = array('separator');
        $buttons[] = array('Rekap Tunggakan','print','ipkd_js');
		*/
        $buttons[] = array('Kembali Ke Daftar Ijin Pemakaian Kekayaan Daerah','','ipkd_js');
		
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("ipkd/daftar/pembayaran_grid/".$id_ipkd),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function ipkd_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}

				if (com=='Cetak Data')
                {
                    location.href='".base_url()."index.php/ipkd/daftar/cetak/'; 
                }

                if (com=='Lihat Jatuh Tempo')
                {
                    location.href='".base_url()."index.php/ipkd/daftar/tunggakan/'; 
                }
				
				if (com=='Tambah')
				{
					location.href='".base_url()."index.php/ipkd/daftar/input_ipkd'; 
				}

				if (com=='Kembali Ke Daftar Ijin Pemakaian Kekayaan Daerah')
				{
					location.href='".base_url()."index.php/ipkd/daftar'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/ipkd/daftar/delete_ipkd')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
		
		/* $data['notification'] = "";
		if($this->session->userdata('notification')!=''){
			$data['notification'] = "
				<script>
					$(document).ready(function() {
						$.growlUI('Pesan :', '".$this->session->userdata('notification')."'); 
					});
				</script>
			";
		}//end if */	
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
		//$this->load->view('grid',$data);
		//$this->load->view('flexigrid',$data);
	}
	
	function pembayaran_grid($id_ipkd)
	{
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('IDBAYAR','TANGGAL_BAYAR','STATUS_BAYAR');
		
		$this->flexigrid->validate_post('IDBAYAR','asc',$valid_fields);

		$records = $this->ipkd_model->get_tanggal_grid($id_ipkd);
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			$items = array();
			$items[] = $row->IDBAYAR;
			$items[] = $no+1;
			$items[] = date("d-m-Y",strtotime($row->TANGGAL_BAYAR));
			if($row->STATUS_BAYAR==0)
			{
				$items[] = '<a href=\''.site_url().'/ipkd/daftar/ganti_status/plus/'.$row->IDBAYAR.'/'.$id_ipkd.'\'>
				<img border=\'0\' src=\''.base_url().'images/main/red.png\'></a>';
			}else{
				$items[] = '<a href=\''.site_url().'/ipkd/daftar/ganti_status/min/'.$row->IDBAYAR.'/'.$id_ipkd.'\'>
				<img border=\'0\' src=\''.base_url().'images/main/green.png\'></a>';
			}

			$record_items[] = $items;
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}

	function ganti_status($status,$id_bayar,$id_ipkd)
	{
			
			if($status=="plus"){
			$data=array(
				'STATUS_BAYAR' => 1
				);

			$this->ipkd_model->update_tgl($id_bayar,$data);
			}
			else{
			$data=array(
				'STATUS_BAYAR' => 0
				);

			$this->ipkd_model->update_tgl($id_bayar,$data);
			}

			redirect('ipkd/daftar/pembayaran/'.$id_ipkd);

	}
}
?>